import {Body, Controller, HttpStatus, Post, Get, Res, Query} from '@nestjs/common';
import {Response} from 'express';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {ApplyCategoryStatus, ResCode} from '../../common/constants';
import {ApplyCategoryService} from "../../service/apply.category.service";
import {ApplyCategoryDto} from "../../dto/apply.category.dto";
import {uuid} from "../../common/utils";


@Controller('apply-admin/category')
export class ApplyCategoryController{
    constructor(private readonly applyCategoryService: ApplyCategoryService) {
    }

    @Post('/add')
    async addCategory(@Body() applyCategoryDto: ApplyCategoryDto, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!applyCategoryDto.name) {
            return new CommonResponseDto(ResCode.ERROR, 'name 必填', null)
        }
        try {
            const category  = await this.applyCategoryService.getOneByName(applyCategoryDto.name)
            if (category) {
                return new CommonResponseDto(ResCode.ERROR, 'name已存在', null)
            }
            applyCategoryDto.status = ApplyCategoryStatus.NORMAL
            applyCategoryDto.id = uuid(32)
            const result = await this.applyCategoryService.createOne(applyCategoryDto)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit')
    async editCategory(@Body() applyCategoryDto: ApplyCategoryDto, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!applyCategoryDto.name) {
            return new CommonResponseDto(ResCode.ERROR, 'name 必填', null)
        }
        try {
            if (![ApplyCategoryStatus.NORMAL, ApplyCategoryStatus.DELETED].includes(applyCategoryDto.status)) {
                return new CommonResponseDto(ResCode.ERROR, 'status状态不正确', null)
            }
            const category  = await this.applyCategoryService.getOneByName(applyCategoryDto.name)
            if (category && category.id !== applyCategoryDto.id) {
                return new CommonResponseDto(ResCode.ERROR, 'name已存在', null)
            }
            const result = await this.applyCategoryService.editOne(applyCategoryDto)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Get('/list')
    async getAllCategoryList(@Query() query: {status?: ApplyCategoryStatus}, @Res({ passthrough: true }) res: Response) {
        console.log('getAllCategoryList', query)
        res.status(HttpStatus.OK);
        try {
            const result = await this.applyCategoryService.getAll(query.status)
            return new CommonResponseDto(ResCode.OK, '成功', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }
}
